# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

from rest_framework import serializers

from auser.models import User
from movie.models import Review, Movie, Comment

# 序列化
from post.models import ReviewComment


class UserSerializer(serializers.HyperlinkedModelSerializer):
	# 外链
	reviews = serializers.HyperlinkedRelatedField(many=True, view_name='review-detail', read_only=True)

	class Meta(object):
		model = User
		# 'reviews'是一个反向关系,用显式字段包含
		fields = ('url', 'id', 'username', 'reviews')


class UserDetailSerializer(serializers.ModelSerializer):
	class Meta(object):
		model = User
		fields = ("username", "email", "date_joined", "telephone", "about_me", "gender")


class MovieSerializer(serializers.ModelSerializer):
	class Meta(object):
		model = Movie
		fields = "__all__"


class MCommentSerializer(serializers.ModelSerializer):
	class Meta(object):
		model = Comment
		fields = "__all__"


class RCommentSerializer(serializers.ModelSerializer):
	review_title = serializers.CharField(source="review.title", help_text='图解名')

	class Meta(object):
		model = ReviewComment
		fields = ("review_title", "user_username", "timestamp", "content")


class MovieCommentSerializer(serializers.ModelSerializer):
	comments = MCommentSerializer(many=True)

	class Meta(object):
		model = Movie
		fields = ("id", "title", "comments")


class ReviewSerializer(serializers.ModelSerializer):
	writer = serializers.CharField(source="user.username")
	movie_name = serializers.CharField(source="movie.title")
	comments_count = serializers.SerializerMethodField()
	praise = serializers.SerializerMethodField()

	def get_praise(self, obj):
		j = obj.reviewpraise_set.count()
		return j

	def get_comments_count(self, obj):
		j = obj.reviewcomment_set.count()
		return j

	class Meta(object):
		model = Review
		fields = ("id", "title", "writer", "movie_name", "comments_count", "praise", "body", "score", "timestamp")
